package com.aiassistant.components;

import javafx.beans.property.SimpleBooleanProperty;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.ToggleButton;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;

/**
 * 自定义切换开关组件
 */
public class ToggleSwitch extends StackPane {
    private final Rectangle back = new Rectangle();
    private final Circle trigger = new Circle();
    private final SimpleBooleanProperty switchedOn = new SimpleBooleanProperty(false);
    
    public ToggleSwitch() {
        initializeSwitch();
        setupListeners();
    }
    
    private void initializeSwitch() {
        // 设置背景样式
        back.getStyleClass().add("toggle-switch-back");
        back.setWidth(40);
        back.setHeight(20);
        back.setArcWidth(20);
        back.setArcHeight(20);
        back.setFill(Color.valueOf("#cccccc"));
        
        // 设置滑块样式
        trigger.setCenterX(10);
        trigger.setCenterY(10);
        trigger.setRadius(8);
        trigger.setFill(Color.WHITE);
        trigger.setEffect(new DropShadow(3, Color.rgb(0, 0, 0, 0.3)));
        
        // 将组件添加到布局
        getChildren().addAll(back, trigger);
        
        // 设置组件大小和位置
        setMinSize(40, 20);
        setPrefSize(40, 20);
        setMaxSize(40, 20);
        setAlignment(Pos.CENTER_LEFT);
        
        // 设置鼠标点击样式
        setStyle("-fx-cursor: hand;");
    }
    
    private void setupListeners() {
        // 添加点击事件处理器
        setOnMouseClicked(event -> {
            switchedOn.set(!switchedOn.get());
        });
        
        // 监听状态改变，更新UI
        switchedOn.addListener((observable, oldValue, newValue) -> {
            if (newValue) {
                trigger.setCenterX(30); // 开启状态，滑块在右侧
                back.setFill(Color.valueOf("#4a86e8")); // 开启状态颜色
            } else {
                trigger.setCenterX(10); // 关闭状态，滑块在左侧
                back.setFill(Color.valueOf("#cccccc")); // 关闭状态颜色
            }
        });
    }
    
    /**
     * 获取开关状态
     */
    public boolean isSelected() {
        return switchedOn.get();
    }
    
    /**
     * 设置开关状态
     */
    public void setSelected(boolean selected) {
        switchedOn.set(selected);
    }
    
    /**
     * 获取状态属性
     */
    public SimpleBooleanProperty selectedProperty() {
        return switchedOn;
    }
} 